package 蓝桥算法;

public class day10 {
    public String reverselworld(String s){
        char[] chars=s.toCharArray();
        chars=removeextraworld(chars);
        reserve(chars,0,chars.length);
        reserveEachworld(chars);
        return new String(chars);
    }
    public void reserve(char[] chars,int left,int right){
        if(right>chars.length){
            System.out.println("输入有错");
            return;
        }
        while (left<right){
            chars[left]^=chars[right];
            chars[right]^=chars[left];
            chars[left]^=chars[right];
            left++;
            right--;
        }
    }
    public void reserveEachworld(char[] chars){
        int start=0;
        for(int end=0;end<=chars.length;end++){
            if(end==chars.length||chars[end]==' '){
                reserve(chars,start,end-1);
                start=end+1;
            }
        }
    }
    public char[] removeextraworld(char[] chars){
        int slow=0;
        for(int fast=0;fast<chars.length;fast++){
            if(chars[fast]!=' '){
                if(slow!=0){
                    chars[slow++]=' ';
                }
                while (fast<chars.length&&chars[fast]!=' '){
                    chars[slow++]=chars[fast++];
                }
            }

        }
        char[] newChars=new char[slow];
        System.arraycopy(chars,0,newChars,0,slow);
        return newChars;
    }

    public static void main(String[] args) {
        day10 b=new day10();
      String s="ss bb d   s";
      String a=b.reverselworld(s);

    }
}
